SETS-CREATION, OPERATIONS, AND SET METHODS

Sets in Python are unordered collections of unique elements. They are useful when you need to work with a collection of items while ensuring that each item appears only once. Here's how you can create sets, perform operations on them, and use some common set methods:

  1. Creating Sets: You can create a set by enclosing elements in curly braces {} or by using the set() constructor. Note that duplicate elements are automatically removed.
python
# Using curly braces my_set = {1, 2, 3} # Using set() constructor another_set = set([3, 4, 5])
  1. Set Operations:

    a. Union: | or union() Returns a new set containing all the elements that are in either of the sets.

    python
    set1 = {1, 2, 3} set2 = {3, 4, 5} # Using | union_set = set1 | set2 print(union_set) # Output: {1, 2, 3, 4, 5} # Using union() union_set = set1.union(set2) print(union_set) # Output: {1, 2, 3, 4, 5}

    b. Intersection: & or intersection() Returns a new set containing elements that are present in both sets.

    python
    set1 = {1, 2, 3} set2 = {3, 4, 5} # Using & intersection_set = set1 & set2 print(intersection_set) # Output: {3} # Using intersection() intersection_set = set1.intersection(set2) print(intersection_set) # Output: {3}

    c. Difference: - or difference() Returns a new set containing elements that are present in the first set but not in the second set.

    python
    set1 = {1, 2, 3} set2 = {3, 4, 5} # Using - difference_set = set1 - set2 print(difference_set) # Output: {1, 2} # Using difference() difference_set = set1.difference(set2) print(difference_set) # Output: {1, 2}

    d. Symmetric Difference: ^ or symmetric_difference() Returns a new set containing elements that are present in either of the sets but not in both.

    python
    set1 = {1, 2, 3} set2 = {3, 4, 5} # Using ^ symmetric_diff_set = set1 ^ set2 print(symmetric_diff_set) # Output: {1, 2, 4, 5} # Using symmetric_difference() symmetric_diff_set = set1.symmetric_difference(set2) print(symmetric_diff_set) # Output: {1, 2, 4, 5}
  2. Set Methods:

    a. set.add(element): Adds an element to the set.

    python
    my_set = {1, 2, 3} my_set.add(4) print(my_set) # Output: {1, 2, 3, 4}

    b. set.remove(element): Removes the specified element from the set. Raises a KeyError if the element is not found.

    python
    my_set = {1, 2, 3} my_set.remove(2) print(my_set) # Output: {1, 3}

    c. set.discard(element): Removes the specified element from the set if it exists, but does nothing if the element is not found.

    python
    my_set = {1, 2, 3} my_set.discard(2) my_set.discard(4) # No error, as 4 is not in the set print(my_set) # Output: {1, 3}

    d. set.pop(): Removes and returns an arbitrary element from the set. Raises a KeyError if the set is empty.

    python
    my_set = {1, 2, 3} removed_element = my_set.pop() print(removed_element) # Output: Randomly selected element (e.g., 1) print(my_set) # Output: The set with the randomly selected element removed

    e. set.clear(): Removes all elements from the set, making it empty.

    python
    my_set = {1, 2, 3} my_set.clear() print(my_set) # Output: set()

    f. set.copy(): Returns a shallow copy of the set.

    python
    my_set = {1, 2, 3} copied_set = my_set.copy() print(copied_set) # Output: {1, 2, 3}

These are some of the basic operations and methods you can use with sets in Python. Sets are particularly useful when you need to handle unique elements and perform set-based operations efficiently.